from pymem import Pymem
import signal
from wcferry import Wcf
from robot import Robot

ADDRS = [0x2FFEAF8, 0x3020E1C, 0x3021AEC, 0x303C4D8, 0x303FEF4, 0x30416EC] 

def fix_version(pm: Pymem):
    WeChatWindll_base = 0
    for m in list(pm.list_modules()):
        path = m.filename
        if path.endswith("WeChatWin.dll"):
            WeChatWindll_base = m.lpBaseOfDll
            break

    for offset in ADDRS:
        addr = WeChatWindll_base + offset
        v = pm.read_uint(addr)
        if v == 0x63090A13:  # 已经修复过了
            continue
        elif v != 0x63090217:  # 不是 3.9.2.23 修复也没用
            raise Exception("别修了，版本不对，修了也没啥用。")

        pm.write_uint(addr, 0x63090A13)

    print("好了，可以扫码登录了")



def main_fun():
    wcf = Wcf(debug=True)

    def handler(sig, frame):
        wcf.cleanup()  # 退出前清理环境
        exit(0)
    # 退出程序
    signal.signal(signal.SIGINT, handler)
    # 初始化机器人
    robot = Robot(wcf)
    robot.log.info(f"WeChatRobot 成功启动···")
    # 接收消息
    robot.enableReceivingMsg()  # 加队列
    # 每天10点发送问题，回答
    # robot.onEveryTime("22:15", ResponseSingleModule.call_back, wcf)
    # 开启定时调度
    robot.keepRunningAndBlockProcess()




if __name__ == "__main__":
    try:
        pm = Pymem("WeChat.exe")
        fix_version(pm)
        main_fun()
        
    except Exception as e:
        print(f"{e}，请确认微信程序已经打开！")
    